package cn.xueden.system.controller;

import cn.xueden.common.result.Result;
import cn.xueden.system.model.form.LoginForm;
import cn.xueden.system.service.IAuthService;
import cn.xueden.system.service.IUserService;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

/**功能描述：登录认证控制器
 * @author:梁志杰
 * @date:2025/6/8
 * @description:cn.xueden.system.controller
 * @version:1.0
 */
@RestController
@RequestMapping("/api/auths")
@RequiredArgsConstructor
public class AuthController {

    private final IAuthService iAuthService;
    @Value("${online-edu.login.captcha.enabled}")
    private boolean captchaEnabled;

    private final IUserService userService;

    /**
     * 用户登录
     *
     * @param request request对象，用户获取sessionId
     * @param loginForm    用户信息
     * @return token
     */
    @PostMapping("/login")
    public Result<String> login(HttpServletRequest request,
                                @Validated @RequestBody LoginForm loginForm) {

        return iAuthService.login(request, loginForm);
    }

    /**
     * 获取图片验证码
     *
     * @param request  request对象，获取sessionId
     * @param response response对象，响应图片
     */
    @GetMapping("/captcha")
    public void getCaptcha(HttpServletRequest request, HttpServletResponse response) {
        iAuthService.getCaptcha(request, response);
    }

    /**
     * 校验验证码
     *
     * @param request request对象，获取sessionId
     * @param code    用户输入的验证码
     * @return 响应结果
     */
    @PostMapping(value = {"/verifyCode/{code}", "/verifyCode/"})
    public Result<String> verifyCode(HttpServletRequest request, @PathVariable(value = "code" , required = false) String code) {
        if (!captchaEnabled) {
            return Result.success();
        }
        return iAuthService.verifyCode(request, code);
    }

    /**
     *  心跳检测
     * @param request
     * @return
     */
    @PostMapping("/track-presence")
    public Result<String> trackPresence(HttpServletRequest request) {
        return iAuthService.sendHeartbeat(request);
    }


}
